package com.piwik.android;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

public class HttpHelper {
	static String SERVER_HOST = "http://analytics.molecular.com/index.php/";
	static int SERVER_PORT = 80;
	
	 public String getListOfSites(String tokenAuth){
	    	HttpEntity entity = null;
	    	HttpClient client = new DefaultHttpClient();
	    	String url = SERVER_HOST + "?module=API&method=SitesManager.getSitesWithAdminAccess&format=csv&token_auth="+tokenAuth;
	    			//4a3c58f49ff124420f42ba74b3b94f7f;
	    	//http://analytics.molecular.com/index.php?module=API&method=SitesManager.getSitesWithAdminAccess&format=Html
	    	HttpGet get = new HttpGet(url);
	    	String keywords = null;
	    	try{
	    		HttpResponse response = client.execute(get);
	    		entity = response.getEntity();
	    		keywords = EntityUtils.toString(entity);
	    	}catch(Exception e){
	    		e.printStackTrace();
	    	}
	    	return keywords;
	    }
	    
	    public String getTokenAuth(String userName, String pwd){
	    	HttpEntity entity = null;
	    	HttpClient client = new DefaultHttpClient();
	    	String url = SERVER_HOST + "?module=API&method=UsersManager.getTokenAuth&userLogin="+userName+"&password="+pwd+"&format=csv";
	    	HttpGet get = new HttpGet(url);
	    	String keywords = null;
	    	try{
	    		HttpResponse response = client.execute(get);
	    		entity = response.getEntity();
	    		keywords = EntityUtils.toString(entity);
	    	}catch(Exception e){
	    		e.printStackTrace();
	    	}
	    	return keywords.replace("value\n", "");
	    }
}
